package com.yiren.manager.ctrl.messhall;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrBeHospitalFee;
import com.yiren.entity.YrCaregiver;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrFoodMeal;
import com.yiren.entity.YrFoodMealsApply;
import com.yiren.entity.YrStaffInfo;
import com.yiren.entity.YrUserVipCard;
import com.yiren.entity.YrUserVipCardRecord;
import com.yiren.entity.YrUserVipCardRent;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.base.YrPersonDetailForm;
import com.yiren.manager.form.elder.YrBeHospitalFeeForm;
import com.yiren.manager.form.elder.YrElderBeHospitalForm;
import com.yiren.manager.form.messhall.YrFoodMealForm;
import com.yiren.manager.form.messhall.YrFoodMealsApplyForm;
import com.yiren.manager.form.vip.YrUserVipCardForm;
import com.yiren.manager.form.vip.YrUserVipCardRecordForm;
import com.yiren.manager.form.vip.YrUserVipCardRentForm;
import com.yiren.manager.service.iface.department.YrCaregiverService;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.elder.YrBeHospitalFeeService;
import com.yiren.manager.service.iface.elder.YrElderBeHospitalService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.messhall.YrFoodMealService;
import com.yiren.manager.service.iface.messhall.YrFoodMealsApplyService;
import com.yiren.manager.service.iface.vip.YrUserVipCardRecordService;
import com.yiren.manager.service.iface.vip.YrUserVipCardRentService;
import com.yiren.manager.service.iface.vip.YrUserVipCardService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("foodMealsApply")
@Controller
public class YrFoodMealsApplyController extends GenericController<YrFoodMealsApplyForm, YrFoodMealsApply>{

	@Autowired
	private YrFoodMealsApplyService selfService;
	
	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrElderBeHospitalService elderBeHospitalService;
	
	@Autowired
	private YrUserVipCardRentService userVipCardRentService;
	
	@Autowired
	private YrUserVipCardService userVipCardService;
	
	@Autowired
	private YrUserVipCardRecordService userVipCardRecordService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@Autowired
	private YrCaregiverService caregiverService;
	
	@Autowired
	private YrBeHospitalFeeService beHospitalFeeService;
	
	@Autowired
	private YrFoodMealService foodMealService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrFoodMealsApplyForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrFoodMealsApplyForm> result = new HTTPResult<YrFoodMealsApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrFoodMealsApplyForm form = tool.mapper("foodMealsApply", YrFoodMealsApplyForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getFoodMealsApplyId()||form.getFoodMealsApplyId().isEmpty()){
			form.setFoodMealsApplyId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
			form.setApproveStatus(0);//设置为未审批状态
			form.setApplyDate(new Date());
		}
		form.setUpdateTime(new Date());
		YrFoodMealsApply entity = ObjectUtils.objectToObject(form, YrFoodMealsApply.class);
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrFoodMealsApplyForm> findAll() throws Exception{
		HTTPResult<YrFoodMealsApplyForm> result = new HTTPResult<YrFoodMealsApplyForm>();
		String sqlWhere = "approve_status=0";
		QueryResult<YrFoodMealsApply> qr = selfService.find(YrFoodMealsApply.class, sqlWhere, new Object[]{});
		
		List<YrFoodMealsApplyForm> formList = new ArrayList<YrFoodMealsApplyForm>();
		
		for(YrFoodMealsApply entity : qr.getResultList()){
			YrFoodMealsApplyForm form = toForm(entity);
			YrFoodMeal foodMeal = foodMealService.findOne(form.getFoodMealsId());
			if(null!=foodMeal){
				form.setFoodMealForm(ObjectUtils.objectToObject(foodMeal,YrFoodMealForm.class));
			}
			YrPersonDetailForm personDetailForm = new YrPersonDetailForm();
			personDetailForm.setName(form.getRelationName());
			personDetailForm.setCardId(form.getRelationCardId());
			personDetailForm.setRelationType(form.getRelationType());
			if(form.getRelationType()==1){
				personDetailForm.setType("长者");
				sqlWhere = "elder_id=?1 AND be_status<>2";
				QueryResult<YrElderBeHospital> qrElderBeHospital = elderBeHospitalService.find(YrElderBeHospital.class, sqlWhere, new Object[]{entity.getRelationId()});
				QueryResult<YrBeHospitalFee> qrBeHospitalFee = beHospitalFeeService.find(YrBeHospitalFee.class, sqlWhere, new Object[]{entity.getRelationId()});
				if(qrBeHospitalFee.getTotalRecord()>0){
					YrBeHospitalFee beHospitalFee = qrBeHospitalFee.getResultList().get(0);
					personDetailForm.setBeHospitalFeeForm(ObjectUtils.objectToObject(beHospitalFee, YrBeHospitalFeeForm.class));
				}
				if(qrElderBeHospital.getTotalRecord()>0){
					YrElderBeHospital elderBeHospital = qrElderBeHospital.getResultList().get(0);
					personDetailForm.setElderBeHospitalForm(ObjectUtils.objectToObject(elderBeHospital, YrElderBeHospitalForm.class));
					if (elderBeHospital.getHouseTypeType() == 1) {// 如果是vip卡入住户型
						YrUserVipCard userVipCard = userVipCardService.findOne(elderBeHospital.getUserVipCardId());
						if (null != userVipCard) {
							//设置用户vip卡
							personDetailForm.setUserVipCardForm(ObjectUtils.objectToObject(userVipCard,YrUserVipCardForm.class));
							sqlWhere = "user_vip_card_id=?1";
							QueryResult<YrUserVipCardRecord> qrUserVipCardRecord = userVipCardRecordService.find(YrUserVipCardRecord.class, sqlWhere, new Object[]{personDetailForm.getUserVipCardForm().getUserVipCardId()});
							if(qrUserVipCardRecord.getTotalRecord()>0){
								//设置用户vip卡使用记录
								personDetailForm.setUserVipCardRecordForm(ObjectUtils.objectToObject(qrUserVipCardRecord.getResultList().get(0), YrUserVipCardRecordForm.class));
							}
							QueryResult<YrUserVipCardRent> qrUserVipCardRent = userVipCardRentService.find(YrUserVipCardRent.class, sqlWhere, new Object[]{personDetailForm.getUserVipCardForm().getUserVipCardId()});
							List<YrUserVipCardRentForm> userVipCardRentForms = new ArrayList<YrUserVipCardRentForm>();
							for(YrUserVipCardRent userVipCardRent : qrUserVipCardRent.getResultList()){
								userVipCardRentForms.add(ObjectUtils.objectToObject(userVipCardRent, YrUserVipCardRentForm.class));
							}
							//设置用户vip卡出租记录
							personDetailForm.setUserVipCardRentForms(userVipCardRentForms);
						}
					}
				}
			}else if (form.getRelationType()==2) {
				personDetailForm.setType("职工");
			}else if(form.getRelationType()==3){
				personDetailForm.setType("护工");
			}
			form.setPersonDetailForm(personDetailForm);
			formList.add(form);
		}
		result.setData(formList);
		return result;
		
	}
	
	
	@RequestMapping("approve")
	@ResponseBody
	public HTTPResult<YrFoodMealsApplyForm> approve(String id,Integer approveStatus) throws MyException{
		HTTPResult<YrFoodMealsApplyForm> result = new HTTPResult<YrFoodMealsApplyForm>();
		if(null==id||null==approveStatus){
			throwExp("参数有误");
		}
		YrFoodMealsApply foodMealsApply = selfService.findOne(id);
		System.out.println("id="+id);
		if(null==foodMealsApply){
			throwExp("查询不到对应的申请记录");
		}
		foodMealsApply.setApproveStatus(approveStatus);
		selfService.save(foodMealsApply);
		
		return result;
	}
	
	@RequestMapping("findPersonDetail")
	@ResponseBody
	public HTTPResult<YrPersonDetailForm> findPersonDetail(String searchVal) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrPersonDetailForm> result = new HTTPResult<YrPersonDetailForm>();
		YrPersonDetailForm form  = new YrPersonDetailForm();
		
		List<YrPersonDetailForm> formList = new ArrayList<YrPersonDetailForm>();
		
		//先查询是不是老者
		String  sqlWhere = "name=?1 OR card_id=?2";
		QueryResult<YrElder> qrElder = elderService.find(YrElder.class, sqlWhere,new Object[]{searchVal,searchVal});
			
		if(qrElder.getTotalRecord()>0){
			YrElder elder = qrElder.getResultList().get(0);
			form.setRelationId(elder.getElderId());
			form.setName(elder.getName());
			form.setCardId(elder.getCardId());
			form.setRelationType(1);
			form.setType("长者");
			sqlWhere = "elder_id=?1 AND be_status<>2";
			QueryResult<YrElderBeHospital> qrElderBeHospital = elderBeHospitalService.find(YrElderBeHospital.class, sqlWhere, new Object[]{elder.getElderId()});
			QueryResult<YrBeHospitalFee> qrBeHospitalFee = beHospitalFeeService.find(YrBeHospitalFee.class, sqlWhere, new Object[]{elder.getElderId()});
			if(qrBeHospitalFee.getTotalRecord()>0){
				YrBeHospitalFee beHospitalFee = qrBeHospitalFee.getResultList().get(0);
				form.setBeHospitalFeeForm(ObjectUtils.objectToObject(beHospitalFee, YrBeHospitalFeeForm.class));
			}
			if(qrElderBeHospital.getTotalRecord()>0){
				YrElderBeHospital elderBeHospital = qrElderBeHospital.getResultList().get(0);
				form.setElderBeHospitalForm(ObjectUtils.objectToObject(elderBeHospital, YrElderBeHospitalForm.class));
				if (elderBeHospital.getHouseTypeType() == 1) {// 如果是vip卡入住户型
					YrUserVipCard userVipCard = userVipCardService.findOne(elderBeHospital.getUserVipCardId());
					if (null != userVipCard) {
						//设置用户vip卡
						form.setUserVipCardForm(ObjectUtils.objectToObject(userVipCard,YrUserVipCardForm.class));
						sqlWhere = "user_vip_card_id=?1";
						QueryResult<YrUserVipCardRecord> qrUserVipCardRecord = userVipCardRecordService.find(YrUserVipCardRecord.class, sqlWhere, new Object[]{form.getUserVipCardForm().getUserVipCardId()});
						if(qrUserVipCardRecord.getTotalRecord()>0){
							//设置用户vip卡使用记录
							form.setUserVipCardRecordForm(ObjectUtils.objectToObject(qrUserVipCardRecord.getResultList().get(0), YrUserVipCardRecordForm.class));
						}
						QueryResult<YrUserVipCardRent> qrUserVipCardRent = userVipCardRentService.find(YrUserVipCardRent.class, sqlWhere, new Object[]{form.getUserVipCardForm().getUserVipCardId()});
						List<YrUserVipCardRentForm> userVipCardRentForms = new ArrayList<YrUserVipCardRentForm>();
						for(YrUserVipCardRent userVipCardRent : qrUserVipCardRent.getResultList()){
							userVipCardRentForms.add(ObjectUtils.objectToObject(userVipCardRent, YrUserVipCardRentForm.class));
						}
						//设置用户vip卡出租记录
						form.setUserVipCardRentForms(userVipCardRentForms);
					}
				}
			}
			formList.add(form);
			result.setData(formList);
			return result;
		}
		
		sqlWhere = "name=?1 OR card_id=?2";
		QueryResult<YrStaffInfo> qrStaffInfo = staffInfoService.find(YrStaffInfo.class, sqlWhere, new Object[]{searchVal,searchVal});
		if(qrStaffInfo.getTotalRecord()>0){
			YrStaffInfo staffInfo = qrStaffInfo.getResultList().get(0);
			form.setName(staffInfo.getName());
			form.setCardId(staffInfo.getCardId());
			form.setRelationType(2);
			form.setType("职工");
			form.setRelationId(staffInfo.getStaffId());
			formList.add(form);
			result.setData(formList);
			return result;
		}
		
		QueryResult<YrCaregiver> qrCaregiver = caregiverService.find(YrCaregiver.class, sqlWhere, new Object[]{searchVal,searchVal});
		if(qrCaregiver.getTotalRecord()>0){
			YrCaregiver caregiver = qrCaregiver.getResultList().get(0);
			form.setName(caregiver.getName());
			form.setCardId(caregiver.getCardId());
			form.setRelationType(3);
			form.setRelationId(caregiver.getCaregiverId());
			form.setType("护工");
			formList.add(form);
			result.setData(formList);
			return result;
		}
		
		throwExp("查不到人任何相关人员信息");
		
		return result;
			
	}
	
	@Override
	public YrFoodMealsApplyForm toForm(YrFoodMealsApply e) throws Exception {
		YrFoodMealsApplyForm form = ObjectUtils.objectToObject(e, YrFoodMealsApplyForm.class);
		return form;
	}

	@Override
	public YrFoodMealsApply toEntity(YrFoodMealsApplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
